//
//  WTMNavigationController.swift
//  shopAssistant
//
//  Created by 周文杰 on 16/10/11.
//  Copyright © 2016年 com.weitaming. All rights reserved.
//

import UIKit

extension UINavigationController {
    
    static func backBarbuttonItem(_ target: AnyObject, action: Selector) -> UIBarButtonItem {
        let button = UIButton.init(frame:CGRect(x: 0, y: 0, width: 44, height: 44))
        button.addTarget(target, action:action, for:UIControl.Event.touchUpInside )
        button.setImage(UIImage.init(named:"NavigationBarBackItemIcon.png"), for:UIControl.State())
//        button.backgroundColor = UIColor.redColor()
        let barbutton = UIBarButtonItem.init(customView: button)
        return barbutton
    }
    static func enlargedRightBarbuttonItem(_ image: String, target: AnyObject, action: Selector) -> [UIBarButtonItem] {
        let button = UIButton.init(frame:CGRect(x: 0, y: 0, width: 44, height: 44))
        button.addTarget(target, action:action, for:UIControl.Event.touchUpInside )
        button.setImage(UIImage.init(named:image), for:UIControl.State())
        let barbutton = UIBarButtonItem.init(customView: button)
        let spaceHolder = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target: nil, action: nil)
        spaceHolder.width = -20
        return [spaceHolder, barbutton]
    }
    static func labeledBarbuttonItem(_ text: String, target: AnyObject, action: Selector) -> UIBarButtonItem {
        //use larger font to wider a bit
        let textSize = UIUtility.calcStringRectSize(text, font: UIFont.systemFont(ofSize: Const.FONT_SIZE+5), refWidth: Const.SCREEN_WIDTH)
        let topLeftButton = UIButton(frame: CGRect(x: 0, y: 0, width: textSize.width, height: 26))
//        topLeftButton.setTitle(text, forState: [.Normal, .Highlighted, .Selected])
//        topLeftButton.setTitleColor(Const.CAMERA_GRAY, forState: [.Normal, .Highlighted, .Selected])
//        topLeftButton.titleLabel!.font = UIFont.systemFontOfSize(Const.FONT_SIZE+2)
        topLeftButton.addTarget(target, action: action, for: .touchUpInside)
        let label = UIUtility.createLabel(text, fontsizeR: 2)
        label.textColor = Const.CAMERA_GRAY
        label.frame = topLeftButton.bounds
        label.textAlignment = .center
        topLeftButton.addSubview(label)
        return UIBarButtonItem(customView: topLeftButton)
    }
    func setNavigationBarTitleDefaultStyle() -> UINavigationController {
        self.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: Const.TitleFont1()]
        return self
    }
    
    func clearBottomLine() {
        self.navigationBar.setBackgroundImage(UIImage(named:"white.png"), for:UIBarMetrics.default)
    }
 
    func showBottomLine() {
        self.navigationBar.setBackgroundImage(UIImage(named:"navibackground_ios7.png"), for:UIBarMetrics.default)
    }
    
}

class WTMNavigationController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationBar.isTranslucent = false
        self.navigationBar.shadowImage = UIImage()//UIUtility.createImageWithColor(UIColor.whiteColor())
        self.showBottomLine()
        self.interactivePopGestureRecognizer?.isEnabled = true
        self.interactivePopGestureRecognizer?.delegate = self

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        super.pushViewController(viewController, animated: animated)
        if self.viewControllers.count > 1 {
            let spaceHolder = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target: nil, action: nil)
            spaceHolder.width = -20
            viewController.navigationItem.leftBarButtonItems = [spaceHolder, UINavigationController.backBarbuttonItem(self, action:#selector(WTMNavigationController.popTopVC))]
        }
    }
    
    override func setViewControllers(_ viewControllers: [UIViewController], animated: Bool) {
        super.setViewControllers(viewControllers, animated: animated)
        for i in 1 ..< self.viewControllers.count {
            let vc = self.viewControllers[i]
            let spaceHolder = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target: nil, action: nil)
            spaceHolder.width = -20
            vc.navigationItem.leftBarButtonItems = [spaceHolder, UINavigationController.backBarbuttonItem(self, action:#selector(WTMNavigationController.popTopVC))]
        }
    }
    
    override func popToViewController(_ viewController: UIViewController, animated: Bool) -> [UIViewController]? {
        let vcs = super.popToViewController(viewController, animated: animated)
        return vcs
    }
    
    @objc func popTopVC() {
        _ = self.popViewController(animated: true)
    }
}
